package com.wang.utils.queueentity;

import java.util.Scanner;

/**
 * @author 10570
 * @date 2021/7/11 21:52
 */

/**
 * 环形对列模拟
 */
public class CycleArrayQueueDemo {
    public static void main(String[] args) {
        CircleArray circleArray = new CircleArray(4); //4个空间，3个有效数据
        char key = '0';//
        System.out.println("测试环形对列模拟数组：");
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop){
            System.out.println("show: 显示队列");
            System.out.println("exit: 退出");
            System.out.println("add: 添加");
            System.out.println("get: 获取");
            System.out.println("head:获取头数据");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    circleArray.showQueue();
                    break;
                case 'a':
                    int value = scanner.nextInt();
                    circleArray.addQueue(value);
                    break;
                case 'h':
                    try {
                        int i = circleArray.headQueue();
                        System.out.printf("头数据%d\n",i);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int queue = circleArray.getQueue();
                        System.out.printf("取出的数据%d\n",queue);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("退出");

    }
}

class CircleArray{
    private int maxSize; // 队列容量
    private int front ; // 指向数组第一个元素
    private int rear; // 指向数组最后一个元素的后一个位置
    private int arr[] ;

    //创建队列的构造器
    public CircleArray(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[maxSize];
        front = 0; //指向对猎头的前一个位置
        rear = 0; //指向队列尾，包含队列尾的数据
    }

    //判断队列是否满
    public boolean isFull(){
        return (rear+1) % maxSize == front;
    }
    //判断队列是否为空
    public boolean isEmpty(){
        return rear == front;
    }

    //添加数据到队列
    public void addQueue(int n){
        //判断队列满
        if (isFull()){
            System.out.println("队列满");
            return;
        }
        arr[rear] = n;
        //判断rear考虑取模
        rear = (rear+1) % maxSize;
    }
    //数据出队列
    public int getQueue(){
        //判断队列空
        if (isEmpty()){
            throw new RuntimeException("队列空");
        }
        //需要分析front是指向对列的第一个元素
        //1、先把front的值放到临时变量
        //2、将front后移,考虑取模
        //3、将临时保存的变量返回
        int value = arr[front];
        front = (front+1)%maxSize;
        return value;
    }

    //显示所有数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列空空");
            return;
        }
        //从front开始遍历
        //遍历元素个数
        //动脑筋
        for (int i = front ;i < front + size() ;i++){
            System.out.printf("arr[%d] = %d \n",i % maxSize,arr[i % maxSize]);
        }
    }

    //求出当前对列有效数据
    public int size(){
        return (rear+maxSize-front) % maxSize;
    }

    //显示头数据
    public int headQueue(){
        if (isEmpty()){
            throw new RuntimeException("队列空");
        }
        return arr[front];
    }
}